package com.xiaoyu.izf.controller.api;

import com.xiaoyu.izf.model.Region;
import com.xiaoyu.izf.model.UserAddress;
import com.xiaoyu.izf.model.UserInfo;
import com.xiaoyu.izf.model.page.FormResult;
import com.xiaoyu.izf.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/user_address")
public class UserAddressApiController extends BaseApiController {
    @Autowired
    private UserAddressService userAddressService;

    @RequestMapping("/regions")
    public FormResult<List<Region>> regions(HttpServletRequest request, int id){
        FormResult<List<Region>> fr = new FormResult<>();
        List<Region> regions = userAddressService.regionsByParentId(id);
        fr.setStatus(200);
        fr.setMessage("ok");
        fr.setData(regions);
        return fr;
    }

    /**
     * 用户的收货地址
     * @return
     */
    @RequestMapping("/my_address")
    public FormResult<List<UserAddress>> userAddress(HttpServletRequest request){
        FormResult<List<UserAddress>> fr = new FormResult<>();
        UserInfo user = getUserByToken(request);
        List<UserAddress> addresses = userAddressService.addressesByUid(user.getId());
        fr.setStatus(200);
        fr.setMessage("ok");
        fr.setData(addresses);
        return fr;
    }

    @RequestMapping("/my_address_one")
    public FormResult<UserAddress> userAddressOne(HttpServletRequest request, int id){
        FormResult<UserAddress> fr = new FormResult<>();
        UserInfo user = getUserByToken(request);
        UserAddress address = userAddressService.addressOne(id);
        fr.setStatus(200);
        fr.setMessage("ok");
        fr.setData(address);
        return fr;
    }

    /**
     * 收货地址
     * @param request
     * @param id 0：新增
     * @param regionProvince
     * @param regionCity
     * @param regionCountry
     * @param addressDetail  地址详情
     * @param receiver 收货人
     * @param receiverPhone 收货人电话
     * @param zipCode 邮编
     * @return
     */
    @RequestMapping("/save")
    public FormResult<UserAddress> userAddressSave(HttpServletRequest request, int id, String regionProvince, String regionCity, String regionCountry, String addressDetail, String receiver, String receiverPhone, String zipCode){
        FormResult<UserAddress> fr = new FormResult<>();
        System.out.println(addressDetail);
        UserInfo user = getUserByToken(request);
        UserAddress address = null;
        if(id==0){//新增
            address = new UserAddress();
        }else{
            address = userAddressService.addressOne(id);
        }
        address.setUid(user.getId());
        address.setRegionProvince(regionProvince);
        address.setRegionCity(regionCity);
        address.setRegionCountry(regionCountry);
        address.setAddressDetail(addressDetail);
        address.setReceiver(receiver);
        address.setReceiverPhone(receiverPhone);
        address.setZipCode(zipCode);
        userAddressService.userAddressSave(address);
        fr.setStatus(200);
        fr.setMessage("ok");
        fr.setData(address);
        return fr;
    }

}
